# -*- coding: utf-8 -*-
"""
Created on Mon Aug 28 14:35:35 2017

@author: xiaoyi
"""
import pandas as pd 
import matplotlib.pyplot as plt
import math
import numpy as np
train = pd.read_csv('2.csv')

#读取数据
from sklearn.cross_validation import train_test_split
selected_features = ['面积（千公顷）','晴','雨','雪']
x_train = train[selected_features]
y_train = train['棉花产量（吨）']
#x_train_data = x_train.iloc[0:94,0:4].values
#x_test = x_train.iloc[40:50].values
x_train_data,x_test_data,y_train_data,y_test_data = train_test_split(x_train,y_train,test_size=0.25,random_state=33)
y_test_data1 = y_test_data.values
x_train_show = train['面积（千公顷）']

#图表显示
plt.scatter(x_train_show,y_train,color='red',marker='o',label = 'changliang')
plt.xlabel('yinshu')
plt.ylabel('changliang')
plt.legend(loc='upper left')
plt.show()

#因为样本数据不是很多，两个模型都是用默认参数，都木有进行调参
from sklearn.ensemble import RandomForestRegressor
rfc = RandomForestRegressor()

from xgboost import XGBRegressor
xgbr = XGBRegressor()

rfc.fit(x_train_data,y_train_data)
y_test_predict1 = rfc.predict(x_test_data)
xgbr.fit(x_train_data,y_train_data)
y_test_predict2 = xgbr.predict(x_test_data)


i = 0
accury = 0 
accury1 = 0
while i<22:
    accury = abs((y_test_predict1[i]-y_test_data1[i]))/y_test_data1[i]
    accury1 = accury1+accury 
    i= i+1
accury2 = accury1/22
accury3 = 1 -accury2
print 'rfcRegressor平均精度为：',accury3

i = 0
accury = 0 
accury1 = 0
while i<22:
    accury = abs((y_test_predict2[i]-y_test_data1[i]))/y_test_data1[i]
    accury1 = accury1+accury 
    i= i+1
accury2 = accury1/22
accury3 = 1 -accury2
print 'XGBRegressor平均精度为：',accury3

  
print '请输入预测因素：'

instance = raw_input()
#instance = np.array(instance)
f = open('train.csv','w')
f.write(instance)
f.close()
x_test = pd.read_csv('train.csv',header =None)
d = rfc.predict(x_test)
print'今年棉花产量为（万吨）： ',d














